package com.tchintech.pay.utils;


import android.annotation.SuppressLint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/3 0003.
 */

public class Signature {

        /**
         * 签名算法
         */
        @SuppressLint("DefaultLocale")
        public static String getSign(Map<String, String> params, String KEY,
                                     String key) throws Exception {
        ArrayList<String> list = new ArrayList<String>();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (entry.getValue() != "") {
                list.add(entry.getKey() + "=" + entry.getValue() + "&");
            }
        }
        int size = list.size();
        String[] arrayToSort = list.toArray(new String[size]);
        Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append(arrayToSort[i]);
        }
        String result = sb.toString();
        result += KEY + "=" + key;


        //   LogTool.e("加密前：" + result);
        result = MD5.getMD5Str(result).toUpperCase();
        // LogUtils.e("加密后：" + result);
        return result;
        }

}
